package com.andreamerla.petslife.manager;

import android.util.Log;
import com.andreamerla.petslife.util.Response;
import com.andreamerla.petslife.util.Utils;
import com.google.api.client.http.*;
import com.google.api.client.http.javanet.NetHttpTransport;

import java.io.IOException;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: a.merla
 * Date: 24/07/13
 * Time: 14.13
 */
public class GoogleFormsManager {
    public static Response submitForm(String formResponseUrl, Map<String, String> formDataMap) {
        try {
            HttpRequestFactory httpRequestFactory = new NetHttpTransport().createRequestFactory();
            UrlEncodedContent urlEncodedContent = new UrlEncodedContent(formDataMap);
            HttpRequest request = httpRequestFactory.buildPostRequest(new GenericUrl(formResponseUrl), urlEncodedContent);
            HttpResponse response = request.execute();

            return response.getStatusCode() == 200 ? Response.OK : Response.ERROR;
        } catch (IOException e) {
            Log.e(Utils.getLogTag(GoogleFormsManager.class), "Error sending form response to URL: " + formResponseUrl);
            e.printStackTrace();
            return Response.ERROR;
        }
    }
}
